package org.xxpay.pay.channel.sqpay;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import org.xxpay.core.common.util.MyLog;
import org.xxpay.core.entity.TransOrder;
import org.xxpay.pay.channel.BaseTransNotify;
import org.xxpay.pay.channel.sqpay.util.CFCARAUtil;

import javax.servlet.http.HttpServletRequest;

@Service
public class SqpayTransNotifyService extends BaseTransNotify {

    private static final MyLog _log = MyLog.getLog(SqpayTransNotifyService.class);

    @Override
    public String getChannelName() {
        return SqpayConfig.CHANNEL_NAME_SQPAY;
    }

    @Override
    public String doNotify(Object notifyData) {
        String logPrefix = "【处理双乾代付回调】";
        _log.info("====== 开始处理双乾代付回调通知 ======");
        HttpServletRequest request = (HttpServletRequest) notifyData;
        TransOrder transOrder;
        //接收参数
        String orderNumber = request.getParameter("orderNumber");
        String amount = request.getParameter("amount");
        String cardNumber = request.getParameter("cardNumber");
        String state = request.getParameter("state");
        String remark = request.getParameter("remark");
        String signature = request.getParameter("signature");

        StringBuffer beforeSignedData = new StringBuffer();
        beforeSignedData.append("orderNumber=" + orderNumber);
        beforeSignedData.append("&amount=" + amount);
        beforeSignedData.append("&cardNumber=" + cardNumber);
        beforeSignedData.append("&state=" + state);
        beforeSignedData.append("&remark=" + remark);

        transOrder = commonService.transOrderService.findByTransOrderId(orderNumber);
        SqpayConfig sqpayConfig = new SqpayConfig(getTransParam(transOrder));
        String publicKeyStr = sqpayConfig.getSqPublicKey();
        try {
            //验签
            boolean verify = CFCARAUtil.verifyMessageByP1AndPubKey(beforeSignedData.toString(), signature, publicKeyStr);
            if (!verify) {
                return buildRet("验证数据没有通过");
            } else {

                if("4".equals(state)) {   // 成功
                    _log.info("{} >>> 转账成功", logPrefix);
                    return updateConfirmSuccess(transOrder, null) ?
                           buildRet("请求成功"): buildRet("处理订单失败");

                }else if("3".equals(state)){ // 失败
                    _log.info("{} >>> 转账失败", logPrefix);
                    return updateConfirmFail(transOrder, "", remark, null) ?
                            buildRet("请求成功"): buildRet("处理订单失败");

                }else{
                    return buildRet("请求成功");
                }
            }
        } catch (Exception e) {
            _log.error(e, logPrefix + "处理异常");
            return buildRet("请求失败");
        }

    }

    /**
     * 生成回调同步响应数据
     *
     * @param msg
     * @return
     */
    String buildRet(String msg) {
        JSONObject object = new JSONObject();
        if ("请求成功".equals(msg)) {
            object.put("result", "success");
        } else {
            object.put("result", "error");
        }
        object.put("msg", msg);
        return object.toJSONString();
    }
}